package com.tuju.jetpackfirstdemo.ui.link;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tuju.jetpackfirstdemo.R;

import java.util.List;

public class PopWindowsUtil {

    private static PopupWindow popupWindow;
    private static TextAdapter popupWindowAdapter;

    /**
     * popupWindow提示框是否正在运行
     *
     * @return popupWindow是否正在运行
     */
    public static boolean isPopupWindowShowing() {
        if (popupWindow != null) {
            Log.i("yanze_popup", "isPopupWindowShowing: " + popupWindow.isShowing());
        }
        return popupWindow != null && popupWindow.isShowing();
    }

    /**
     * popupWindow提示框消失方法
     */
    public static void popupWindowDismiss() {
        if (isPopupWindowShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
            popupWindowAdapter = null;
        }
    }

    public static void showPopupWindow(List<String> datas, final View tagerView, final PopupSelectCallBack callBack) {
        if (isPopupWindowShowing()) {
            popupWindowDismiss();
            return;
        }
        if (popupWindow != null) {
            popupWindowAdapter.clear();
            popupWindowAdapter.addAll(datas);
            popupWindowAdapter.notifyDataSetChanged();
        } else {
            popupWindow = new PopupWindow(tagerView.getContext());
            View contentView = LayoutInflater.from(tagerView.getContext()).inflate(R.layout.dialog_list, null);
            RecyclerView recyclerView = contentView.findViewById(R.id.rv_text);
            FrameLayout frameLayout = contentView.findViewById(R.id.fl_pop_parent);
            frameLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
            recyclerView.setLayoutManager(new LinearLayoutManager(tagerView.getContext()));
            popupWindowAdapter = new TextAdapter();
            popupWindowAdapter.addAll(datas);
            popupWindowAdapter.setCallBack(new TextAdapter.TextAdapterCallBack() {
                @Override
                public void onItemClickCallback(String content, int position) {
                    callBack.selectItem(content);
                    popupWindow.dismiss();
                }
            });
            recyclerView.setAdapter(popupWindowAdapter);

            popupWindow.setContentView(contentView);
            popupWindow.setAnimationStyle(R.style.PopWindowAnimation);
            popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setBackgroundDrawable(null);//设置空背景 不设置背景图片点击外部不会消失
            popupWindow.setOutsideTouchable(true);//点击外部消失
        }

        //show
        popupWindow.showAsDropDown(tagerView);
    }

    interface PopupSelectCallBack{
        void selectItem(String str);
    }

}
